const {mysql} = require('../qcloud')

module.exports = async (ctx) => {
    const {type, page, pageSize, sort, sortType} = ctx.request.query

    let goodsList = []
    if (type === 'new') {
        goodsList = await mysql('nideshop_goods')
            .where({is_new: 1})
            .select()
            .orderBy(sort, sortType)
            .limit(pageSize)
            .offset(Number(page) * pageSize)
    } else if (type === 'hot') {
        goodsList = await mysql('nideshop_goods')
            .where({is_hot: 1})
            .select()
            .orderBy(sort, sortType)
            .limit(pageSize)
            .offset(Number(page) * pageSize)
    } else {
        goodsList = await mysql('nideshop_goods')
            .where('name', 'like', '%' + type + '%')
            .orderBy(sort, sortType)
            .limit(pageSize)
            .offset(Number(page) * pageSize)
            .select()
    }

    ctx.state.data = {
        goodsList
    }
}
